/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/5 14:05
 * 仅仅用于学习性质，非正确二叉树创建
 */

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 结点的创建
 */
class TreeNode {
    public char val;
    public TreeNode left;//左孩子的引用
    public TreeNode right;//右孩子的引用
    public TreeNode(char val){
        this.val = val;
    }
}

public class BinaryTree {
    public TreeNode root;//二叉树根结点

    public TreeNode createTree(){

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
       // TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;
        TreeNode root = A;
        return root;
    }

    //前序遍历

    /**
     * 非递归实现
     * @param root
     * @return
     */
   /** public List<Character> preOrder(TreeNode root){
        List<Character> retlist = new ArrayList<>();
        if(root == null){
            return retlist;
        }
        retlist.add(root.val);
        List<Character>leftTree = preOrder(root.left);
        retlist.addAll(leftTree);

        List<Character>rightTree = preOrder(root.right);
        retlist.addAll(rightTree);

        return retlist;
    }*/

    void preOrder(TreeNode root){
        int count = 0;
        if(root != null){
            System.out.print(root.val + " ");
            preOrder(root.left);
            preOrder(root.right);
        }else{
            return;
        }
    }

    //中序遍历
    void midOrder(TreeNode root){
        if(root == null){
            return;
        }
          midOrder(root.left);
        System.out.print(root.val+" ");
        midOrder(root.right);
    }

    //后序遍历
    void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    int count = 0;
    int size(TreeNode root){
        if(root == null){
            return 0;
        }
        count++;
        size(root.left);
        size(root.right);
        return count;
    }

    /**
     * 子问题求解结点个数
     * * @param root
     * @return
     */
    int size1(TreeNode root){
        if(root == null){
            return 0;
        }
        return size1(root.left)+size1(root.right)+1;

    }

    int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
       if(root.left == null&&root.right==null){
          count++;
       }
       return count;
    }

    /**
     * 左子树叶子结点+右子数叶子结点
     * @param root
     * @return
     */
    int getLeafNodeCount1(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null&&root.right==null){
            return 1;
        }
        return getLeafNodeCount1(root.left)+getLeafNodeCount1(root.right);
    }

    //第k层的结点个数

    int getKLeveNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(root.left == null&&root.right==null && k==1){
            return 1;
        }
        return getKLeveNodeCount(root.left,k-1)+getKLeveNodeCount(root.right,k-1);
    }

    //获取二叉树的高度

    /**
     * 时间复杂度：O(n)
     * 空间复杂度：O(log n)
     * @param root
     * @return
     */
    int getHight(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftHight = getHight(root.left);//目的是减少递归次数，减小时间复杂度
        int rightHight = getHight(root.right);//同上
        return leftHight>rightHight ? leftHight+1:rightHight+1;
    }

    /**
     * 查找二叉树中元素
     * @param root
     * @param val
     * @return
     */
   TreeNode find(TreeNode root,char val){
        if(root == null) return null;

       if(root.val == val) {
           return root;
       }
       TreeNode ret = find(root.left,val);
       if(ret != null){
           return ret;
       }
       ret = find(root.right,val);
       if(ret != null){
           return ret;
       }
        return null;
   }

    /**
     * 判断是不是完全二叉树
     * @param root
     * @return
     */
   boolean isCompleteTree(TreeNode root){
       if(root == null) return true;
       Queue<TreeNode>queue = new LinkedList<>();
       queue.offer(root);
       while(!queue.isEmpty()){
           TreeNode cur = queue.poll();
           if(cur != null){
               queue.offer(cur.left);
               queue.offer(cur.right);
           }else{
               break;
           }
       }
       while (!queue.isEmpty()){
                TreeNode top = queue.peek();
                if(top != null){
                    return false;
                }
                queue.poll();
       }
       return true;
   }

}
